package com.fiveflavors.suittar.gui.mainpanel.view;


import java.net.URL;

import com.fiveflavors.suittar.SuittarCommand;
import com.fiveflavors.suittar.gui.utils.Converter;
import com.fiveflavors.suittar.resources.SuittarResourceManager;

import twitter4j.Status;

public abstract class SuittarTimeLineView {

	private SuittarTimeLineViewBean timeLineViewBean = null;

	public SuittarTimeLineView() {
	}

	public void setSuitterTimeLineViewBean(SuittarTimeLineViewBean timeLineViewBean) {
		this.timeLineViewBean = timeLineViewBean;		
	}

	public StringBuffer getContents() {

		StringBuffer htmlContents = new StringBuffer();

		htmlContents.append("<HTML>");
		htmlContents.append("<BODY>");

		htmlContents.append("<TABLE ALIGN=\"CENTER\" WIDTH=\"100%\">");
		htmlContents.append("<TR><TD>");

		//View Header
		htmlContents.append(getHeaderContents(timeLineViewBean));

		htmlContents.append("</TD></TR>");
		htmlContents.append("<TR><TD>");
		htmlContents.append("<HR align=\"CENTER\" size=\"1\" width=\"100%\">");
		htmlContents.append("</TD></TR>");
		htmlContents.append("</TABLE>");

		//Main Contents(Header) - <TABLE>Tag
		htmlContents.append(getStatusTableDeclare());

		//Main Contents (Row) - <TR><TD></TD></TR>
		for(Status status: timeLineViewBean.getStatusList()) {
			htmlContents.append(getStatusRowContents(status,timeLineViewBean));
		}

		//Main Contents(Closer) - </TABLE>Tag
		htmlContents.append(getStatusTableCloser());

		//View Footer
		htmlContents.append(getFooterContents(timeLineViewBean));
		
		htmlContents.append("</BODY>");
		htmlContents.append("</HTML>");
		
		return htmlContents;
	}


	public StringBuffer getStatusTableDeclare() {
		return new StringBuffer("<TABLE WIDTH=\"100%\">");
	}
	
	public StringBuffer getStatusTableCloser() {
		return new StringBuffer("</TABLE>");
	}
	
	public StringBuffer getStatusRowContents(Status status,SuittarTimeLineViewBean timeLineViewBean) {

		StringBuffer buf = new StringBuffer();
		String replayBGColor = "";
		if (status.getText().indexOf("@" + timeLineViewBean.getMyUser().getScreenName()) > -1) {
			replayBGColor = " BGCOLOR=\"#ffdead\"";
		}

		buf.append("<TR>");
		
		buf.append("<TD ROWSPAN=\"3\" WIDTH=\"48\" valign=\"top\"" +  replayBGColor + ">");
		buf.append("<A HREF=\"http://suittar20/command?command=" + SuittarCommand.COMMAND_SHOW_PROFILE + "&id=" + status.getUser().getScreenName() + "\">");
		buf.append(Converter.convertURL2ImgTagHtml(status.getUser().getProfileImageURL()));
		buf.append("</A>");			
		buf.append("</TD>");
		
		buf.append("<TD" + replayBGColor + ">");		

		URL atmarkURL = SuittarResourceManager.getResourceUrl(SuittarResourceManager.ICON_ATMARK);
		buf.append("&nbsp;<A HREF=\"http://suittar20/setAtmark?id=" + status.getUser().getScreenName() + "\">");
		buf.append(Converter.convertURL2ImgTagHtml(atmarkURL));
		buf.append("</A>");			
		
		if (status.getUser().getURL() != null) {
			URL hpURL = SuittarResourceManager.getResourceUrl(SuittarResourceManager.ICON_HOMEPAGE);
			String hp =  status.getUser().getURL().toString();
			if (!hp.startsWith("http://") && !hp.startsWith("https://")) {
				hp = "http://" + hp;
			}
			buf.append("&nbsp;<A HREF=\"" + hp + "\">");
			buf.append(Converter.convertURL2ImgTagHtml(hpURL));
			buf.append("</A>");			
		}
		else {
			URL hpNonURL = SuittarResourceManager.getResourceUrl(SuittarResourceManager.ICON_HOMEPAGE_NONE);
			buf.append("&nbsp;" + Converter.convertURL2ImgTagHtml(hpNonURL));			
		}
		
		buf.append("&nbsp;&nbsp;<A HREF=\"http://suittar20/command?command=" + SuittarCommand.COMMAND_SHOW_PROFILE + "&id=" + status.getUser().getScreenName() + "\">");
		buf.append(status.getUser().getScreenName());
		buf.append("</A>");
		
		buf.append("</TD>");
		buf.append("</TR>");

		buf.append("<TR>");
		buf.append("<TD" + replayBGColor + ">");
		buf.append(Converter.addAnchorTagInText(status.getText()));			
		buf.append("</TD>");
		buf.append("</TR>");

		buf.append("<TR>");
		buf.append("<TD align=\"right\"" + replayBGColor + ">");
		buf.append(Converter.convertUTCString2JSTString(status.getCreatedAt()));
		buf.append("</TD>");
		buf.append("</TR>");
		return buf;
	}
	
	public abstract StringBuffer getHeaderContents(SuittarTimeLineViewBean suittarTimeLineViewBean);

	public abstract StringBuffer getFooterContents(SuittarTimeLineViewBean suittarTimeLineViewBean);

}
